/**
 *******************************************************************************
 * @file gpio_driver.h
 * @author .ISS_AUTO (hudandan@issauto.com)
 * @version 0.1
 * @brief 功能: DI检测 + 按键检测(可使能开启)
 *        MCU: N32G45x
 *
 * @date 2023-05-24    创建
 *       2023-06-07    移植MCU-N32G45x
 *       2025-03-19    优化宏定义
 *
 * @copyright Copyright (c) 2023 ISSAUTO TECH Co., Ltd. All rights reserved.
 *
 *******************************************************************************
 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __USER_GPIO_DRIVER_H
#define __USER_GPIO_DRIVER_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "user_gpio_conf.h"

/* Exported types ------------------------------------------------------------*/
enum {
    GPIO_LEVEL_INVALID = 0xFF,
    GPIO_LEVEL_LOW  = 0,
    GPIO_LEVEL_HIGH = 1,
};


/** @addtogroup 每个DI/DO对应1个全局的结构体变量。
  * @{
  */

typedef unsigned char (*GetInputStatusFunc)(void);

typedef struct {
    GetInputStatusFunc GetBit; /* 函数指针：指向读取gpio状态的函数,1表示高电平 */

    unsigned char Status; /* gpio状态（高低电平） */
    unsigned char Count; /* 滤波器计数器 */

    #if (BUTTONS_CNT_NUM)
    unsigned char ButtonIdx; /* Button ID */
    #endif

} InputTypeDef_t;

/**
  * @}
  */


/** @addtogroup
  * @{
  */

#if (BUTTONS_CNT_NUM)

typedef struct {
    unsigned short LongCount; /* 长按计数器 */
    unsigned short LongTime;  /* 按键按下持续时间, 0表示不检测长按 */

    unsigned char RepeatSpeed; /* 连续按键周期 */
    unsigned char RepeatCount; /* 连续按键计数器 */
} ButtonTypeDef_t;

#define KEY_FIFO_SIZE	4

typedef struct {
    unsigned char Buf[KEY_FIFO_SIZE];	/* 键值缓冲区 */
    unsigned char Read;					/* 缓冲区读指针 */
    unsigned char Write;				/* 缓冲区写指针 */
} KEY_FIFO_T; /* 按键FIFO变量 */

#endif

/**
  * @}
  */

/* Exported constants --------------------------------------------------------*/
#ifndef ON
#define ON     1
#endif

#ifndef OFF
#define OFF    0
#endif

/**
 * @brief 输入断开检测滤波时间, 单位ms。
 *        只有连续检测到 INPUT_FILTER_CNT*GPIO_SCAN_TIME(ms) 状态不变才认为有效
 */
#define GPIO_SCAN_BASE_TIMES    (1) //!< Base time frequency

#define GPIO_SCAN_TIME          (2) //!<  /= GPIO_SCAN_BASE_TIMES
#define INPUT_FILTER_CNT        (2)

/* Exported macro ------------------------------------------------------------*/

/* Exported functions ------------------------------------------------------- */

/** @brief reset the specified GPIO pin input status  */
void reset_gpio_scan_bit(unsigned char gpio_idx);

/** @brief get the specified GPIO pin input status */
unsigned char get_gpio_scan_bit(unsigned char gpio_idx);

/** @brief get the specified GPIO pin input current status */
unsigned char get_gpio_bit_status(unsigned char gpio_idx);

/** @brief scan all gpio defined input bit */
void user_gpio_driver_scan(void);

/** @brief gpio initialization */
void user_gpio_driver_init(void);

/** @brief gpio timer tick */
void user_gpio_driver_timer_tick_isr(void);

#if (BUTTONS_CNT_NUM)
/** * @brief Get the Key object 从按键FIFO缓冲区读取一个键值 */
unsigned char user_button_value_pop(void);

/** * @brief 清空按键FIFO缓冲区*/
void user_button_value_clear(void);

/** * @brief Set the Key Param object 设置按键参数 */
void user_button_Param_Set(unsigned char gpio_idx, unsigned char keyID, unsigned short LongTime, unsigned char RepeatSpeed);
#endif


#ifdef __cplusplus
}
#endif

#endif /* __USER_GPIO_DRIVER_H */

/* END OF FILE ---------------------------------------------------------------*/
